// *****************************************************
// Aitrox Information Technology
// http://www.proxima-ai.com
// Copyright 2021 Aitrox. All rights reserved.
//
// Filename: breast.go
// CreateDate: 2023-05-06
// Author: zjp
// Description: 获取乳腺病灶数据
// ******************************************************
package export

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

type BreastNidusInfo struct {
	*NidusExtInfo
	CheckStatus         uint32  `json:"checkStatus" gorm:"Column:checkStatus"`
	ImageIsDelete       uint16  `json:"imageIsDelete" gorm:"Column:imageIsDelete"`
	LRDiameter          float32 `json:"lrDiameter" gorm:"Column:lrDiameter"`
	FBDiameter          float32 `json:"fbDiameter" gorm:"Column:fbDiameter"`
	Area                float32 `json:"area" gorm:"Column:area"`
	Malignant           int     `json:"malignant" gorm:"Column:malignant"`
	MalignantProb       float32 `json:"malignantProb" gorm:"Column:malignantProb"`
	EchoGenicity        int     `json:"echoGenicity" gorm:"Column:echoGenicity"`
	MicroCalcifications int     `json:"microCalcifications" gorm:"Column:microCalcifications"`
	Shape               int     `json:"shape" gorm:"Column:shape"`
	Margin              int     `json:"margin" gorm:"Column:margin"`
	Orientation         int     `json:"orientation" gorm:"Column:orientation"`
	Acoustic            int     `json:"acoustic" gorm:"Column:acoustic"`
	Pos                 int     `json:"pos" gorm:"Column:pos"`
	X1                  int     `json:"x1" gorm:"Column:x1"`
	Y1                  int     `json:"y1" gorm:"Column:y1"`
	X2                  int     `json:"x2" gorm:"Column:x2"`
	Y2                  int     `json:"y2" gorm:"Column:y2"`
	Glitch              int     `json:"glitch" gorm:"Column:glitch"`
	Vague               int     `json:"vague" gorm:"Column:vague"`
	Microlobulation     int     `json:"microlobulation" gorm:"Column:microlobulation"`
	Angled              int     `json:"angled" gorm:"Column:angled"`
	Hyperecho           int     `json:"hyperecho" gorm:"Column:hyperecho"`
}

var (
	ETubercleShapeCircular  = 0 //圆形
	ETubercleShapeEllipse   = 1 //椭圆形
	ETubercleShapeIrregular = 2 //不规则
)
var (
	EBreastTubercleCalcificNone = 0 //无微钙化
	EBreastTubercleCalcifics    = 1 //有微钙化
)
var (
	EBreastTubercleEdgeFitting   = 0 //光整
	EBreastTubercleEdgeNoFitting = 1 //不光整
)

/**
 * @brief 获取乳腺病灶数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetBreastExportData(db *gorm.DB, patientIds string) ([]*BreastNidusInfo, error) {
	return getBreastExportData(db, patientIds, "t_breast_tubercle_info")
}

/**
 * @brief 获取乳腺病灶AI数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetBreastExportDataAi(db *gorm.DB, patientIds string) ([]*BreastNidusInfo, error) {
	return getBreastExportData(db, patientIds, "t_breast_tubercle_info_ai")
}

func getBreastExportData(db *gorm.DB, patientIds, tbn string) ([]*BreastNidusInfo, error) {
	var nidus []*BreastNidusInfo

	var query string
	query = "SELECT t1.patient_name, t1.check_id, t1.patient_id, t1.timeStamp, t1.sex, t1.age, t2.imageNTPStamp, t2.checkStatus, t2.isDelete as imageIsDelete, "
	query += "t3.lrDiameter, t3.fbDiameter, t3.area, t3.malignant, t3.malignantProb, t3.echoGenicity, t3.microCalcifications, t3.shape, "
	query += "t3.margin, t3.orientation, t3.acoustic, t3.pos, t3.x1, t3.y1, t3.x2, t3.y2, t3.glitch, t3.vague, t3.microlobulation, t3.angled, t3.hyperecho"
	query += " FROM t_patient_info t1"
	query += " LEFT JOIN t_image_info t2 on t1.id=t2.patientId"
	query += fmt.Sprintf(" INNER JOIN %s t3 on t2.id=t3.imageId and t1.id=t3.patientId", tbn)
	//导出患者ID
	query += fmt.Sprintf(" WHERE t1.id in (%s)", patientIds)

	res := db.Raw(query).Scan(&nidus)
	if res.Error != nil {
		return nil, res.Error
	}

	return nidus, nil
}
